<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人中心 - 垃圾分类系统</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 15px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.1);
            overflow: hidden;
        }

        .header {
            background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }

        .nav-tabs {
            display: flex;
            background: #f8f9fa;
            border-bottom: 1px solid #dee2e6;
        }

        .nav-tab {
            flex: 1;
            padding: 15px;
            text-align: center;
            background: none;
            border: none;
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s;
        }

        .nav-tab.active {
            background: white;
            border-bottom: 3px solid #4CAF50;
            color: #4CAF50;
        }

        .nav-tab:hover {
            background: #e9ecef;
        }

        .tab-content {
            display: none;
            padding: 30px;
        }

        .tab-content.active {
            display: block;
        }

        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .stat-card {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 25px;
            border-radius: 10px;
            text-align: center;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }

        .stat-card h3 {
            font-size: 2em;
            margin-bottom: 10px;
        }

        .stat-card p {
            font-size: 1.1em;
            opacity: 0.9;
        }

        .level-card {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 25px;
            border-radius: 10px;
            text-align: center;
            margin-bottom: 30px;
        }

        .level-card h2 {
            font-size: 2.5em;
            margin-bottom: 10px;
        }

        .achievements-grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }

        .achievement-card {
            background: white;
            border: 2px solid #e9ecef;
            border-radius: 10px;
            padding: 20px;
            text-align: center;
            transition: all 0.3s;
        }

        .achievement-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
        }

        .achievement-icon {
            width: 60px;
            height: 60px;
            margin: 0 auto 15px;
            border-radius: 50%;
            background: #4CAF50;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
            color: white;
        }

        .achievement-icon img {
            width: 40px;
            height: 40px;
        }

        .points-history {
            background: #f8f9fa;
            border-radius: 10px;
            padding: 20px;
        }

        .points-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px;
            border-bottom: 1px solid #dee2e6;
        }

        .points-item:last-child {
            border-bottom: none;
        }

        .points-positive {
            color: #28a745;
            font-weight: bold;
        }

        .points-negative {
            color: #dc3545;
            font-weight: bold;
        }

        .checkin-btn {
            background: linear-gradient(135deg, #4CAF50 0%, #45a049 100%);
            color: white;
            border: none;
            padding: 15px 30px;
            border-radius: 25px;
            font-size: 16px;
            cursor: pointer;
            transition: all 0.3s;
            margin-bottom: 20px;
        }

        .checkin-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(76, 175, 80, 0.3);
        }

        .checkin-btn:disabled {
            background: #6c757d;
            cursor: not-allowed;
            transform: none;
            box-shadow: none;
        }

        .back-btn {
            display: inline-block;
            background: #6c757d;
            color: white;
            text-decoration: none;
            padding: 10px 20px;
            border-radius: 5px;
            margin-bottom: 20px;
            transition: all 0.3s;
        }

        .back-btn:hover {
            background: #5a6268;
            text-decoration: none;
            color: white;
        }

        @media (max-width: 768px) {
            .nav-tabs {
                flex-direction: column;
            }
            
            .stats-grid {
                grid-template-columns: 1fr;
            }
            
            .achievements-grid {
                grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>个人中心</h1>
            <p>欢迎回来，{{ user.username }}！</p>
        </div>

        <div class="nav-tabs">
            <button class="nav-tab active" onclick="showTab('overview')">概览</button>
            <button class="nav-tab" onclick="showTab('achievements')">成就</button>
            <button class="nav-tab" onclick="showTab('points')">积分记录</button>
            <button class="nav-tab" onclick="showTab('stats')">统计数据</button>
        </div>

        <div id="overview" class="tab-content active">
            <a href="{{ url_for('index') }}" class="back-btn">← 返回首页</a>
            
            <div class="level-card">
                <h2>{{ user_points.level_name }}</h2>
                <p>等级 {{ user_points.level }} | 总积分 {{ user_points.total_earned }}</p>
            </div>

            <button id="checkinBtn" class="checkin-btn" onclick="dailyCheckin()">每日签到</button>

            <div class="stats-grid">
                <div class="stat-card">
                    <h3>{{ user_points.points }}</h3>
                    <p>当前积分</p>
                </div>
                <div class="stat-card">
                    <h3>{{ user_stats.total_predictions }}</h3>
                    <p>识别次数</p>
                </div>
                <div class="stat-card">
                    <h3>{{ "%.1f" | format(user_stats.accuracy_rate * 100) }}%</h3>
                    <p>识别准确率</p>
                </div>
                <div class="stat-card">
                    <h3>{{ user_stats.consecutive_days }}</h3>
                    <p>连续使用天数</p>
                </div>
            </div>
        </div>

        <div id="achievements" class="tab-content">
            <h2>我的成就</h2>
            <div class="achievements-grid">
                {% for user_achievement, achievement in user_achievements %}
                <div class="achievement-card">
                    <div class="achievement-icon">
                        {% if achievement.icon_url %}
                            <img src="{{ achievement.icon_url }}" alt="{{ achievement.name }}">
                        {% else %}
                            🏆
                        {% endif %}
                    </div>
                    <h4>{{ achievement.name }}</h4>
                    <p>{{ achievement.description }}</p>
                    <small>获得时间: {{ user_achievement.achieved_at.strftime('%Y-%m-%d') }}</small>
                </div>
                {% endfor %}
                
                {% if not user_achievements %}
                <div class="achievement-card">
                    <div class="achievement-icon">🎯</div>
                    <h4>暂无成就</h4>
                    <p>开始使用系统来获得你的第一个成就吧！</p>
                </div>
                {% endif %}
            </div>
        </div>

        <div id="points" class="tab-content">
            <h2>积分记录</h2>
            <div class="points-history">
                {% for point in recent_points %}
                <div class="points-item">
                    <div>
                        <strong>{{ point.reason }}</strong>
                        {% if point.description %}
                            <br><small>{{ point.description }}</small>
                        {% endif %}
                        <br><small>{{ point.created_at.strftime('%Y-%m-%d %H:%M') }}</small>
                    </div>
                    <div class="{% if point.points_change > 0 %}points-positive{% else %}points-negative{% endif %}">
                        {% if point.points_change > 0 %}+{% endif %}{{ point.points_change }}
                    </div>
                </div>
                {% endfor %}
                
                {% if not recent_points %}
                <div class="points-item">
                    <div>暂无积分记录</div>
                    <div>0</div>
                </div>
                {% endif %}
            </div>
        </div>

        <div id="stats" class="tab-content">
            <h2>详细统计</h2>
            <div class="stats-grid">
                <div class="stat-card">
                    <h3>{{ user_stats.total_predictions }}</h3>
                    <p>总识别次数</p>
                </div>
                <div class="stat-card">
                    <h3>{{ user_stats.correct_predictions }}</h3>
                    <p>正确识别次数</p>
                </div>
                <div class="stat-card">
                    <h3>{{ user_stats.favorite_category or '暂无' }}</h3>
                    <p>最常识别类型</p>
                </div>
                <div class="stat-card">
                    <h3>{{ user_stats.last_active_date.strftime('%Y-%m-%d') if user_stats.last_active_date else '从未' }}</h3>
                    <p>最后活跃日期</p>
                </div>
            </div>
        </div>
    </div>

    <script>
        function showTab(tabName) {
            // 隐藏所有标签页内容
            const contents = document.querySelectorAll('.tab-content');
            contents.forEach(content => content.classList.remove('active'));
            
            // 移除所有标签页按钮的激活状态
            const tabs = document.querySelectorAll('.nav-tab');
            tabs.forEach(tab => tab.classList.remove('active'));
            
            // 显示选中的标签页内容
            document.getElementById(tabName).classList.add('active');
            
            // 激活选中的标签页按钮
            event.target.classList.add('active');
        }

        function dailyCheckin() {
            const btn = document.getElementById('checkinBtn');
            btn.disabled = true;
            btn.textContent = '签到中...';
            
            fetch('/checkin', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                }
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    alert(data.message);
                    btn.textContent = '已签到';
                    // 刷新页面以更新积分显示
                    location.reload();
                } else {
                    alert(data.error || '签到失败');
                    btn.disabled = false;
                    btn.textContent = '每日签到';
                }
            })
            .catch(error => {
                console.error('签到错误:', error);
                alert('签到失败，请稍后重试');
                btn.disabled = false;
                btn.textContent = '每日签到';
            });
        }

        // 检查今天是否已签到
        window.onload = function() {
            // 这里可以添加检查今日签到状态的逻辑
            // 暂时简化处理
        };
    </script>
</body>
</html>